package o2o.config.jms.config;

import o2o.config.jms.listener.TopicMessageListener;
import org.apache.activemq.command.ActiveMQQueue;
import org.apache.activemq.command.ActiveMQTopic;
import org.apache.activemq.spring.ActiveMQConnectionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.connection.CachingConnectionFactory;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.listener.DefaultMessageListenerContainer;

import javax.jms.ConnectionFactory;
import javax.jms.Destination;

/**
 * Created with IntelliJ IDEA.
 * User: Jahnke [1029777564@qq.com]
 * Date: 2018-05-04 21:14
 * To change this template use File | Settings | File Templates.
 */
@Configuration
public class ActiveMQConfig2 {

    @Autowired
    private JMSProperties jmsProperties;
    @Autowired
    private ConnectionFactory connectionFactory;
    @Autowired
    private Destination toppicDestination;
    @Autowired
    private TopicMessageListener topicMessageListener;

    private static String topicUrl;
    private static String queueUrl;
    private static String url;

    public void initLoad() {
        topicUrl = jmsProperties.getNotifyTopic();
        queueUrl = jmsProperties.getNotifyQueue();
        url = jmsProperties.getUrl();

        System.out.println("jms-url:" + url);
    }

    // 配置一个消息队列
    @Bean(name = "toppicDestination")
    public Destination destination() {
        return new ActiveMQTopic(topicUrl);
    }

    @Bean(name = "notifyQueue")
    public Destination notifyQueue() {
        return new ActiveMQQueue(queueUrl);
    }

    @Bean
    public ConnectionFactory connectionFactory() {
        initLoad();
        ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory();
        activeMQConnectionFactory.setBrokerURL(url);
        return activeMQConnectionFactory;
    }

    @Bean(name = "jmsTemplate")
    public JmsTemplate jmsTemplate() {

        CachingConnectionFactory cachingConnectionFactory = new CachingConnectionFactory();
        cachingConnectionFactory.setTargetConnectionFactory(connectionFactory);
        cachingConnectionFactory.setSessionCacheSize(100);

        JmsTemplate jmsTemplate = new JmsTemplate();
        jmsTemplate.setConnectionFactory(cachingConnectionFactory);

        return jmsTemplate;
    }

    @Bean(name = "topicContainer")
    public DefaultMessageListenerContainer topicContainer() {
        DefaultMessageListenerContainer defaultMessageListenerContainer = new DefaultMessageListenerContainer();
        defaultMessageListenerContainer.setConnectionFactory(connectionFactory);
        defaultMessageListenerContainer.setDestination(new ActiveMQTopic(topicUrl));
        defaultMessageListenerContainer.setMessageListener(topicMessageListener);
        defaultMessageListenerContainer.setPubSubDomain(true);

        return defaultMessageListenerContainer;
    }
}
